package cn.smbms.service.bill;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.bill.BillDao;
import cn.smbms.dao.bill.BillDaoImpl;
import cn.smbms.dao.bill.BillMapper;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.tools.MyBatisUtil;

@Service("billService")
public class BillServiceImpl implements BillService {
	
	private Logger log = Logger.getLogger(BillServiceImpl.class);
	
	@Resource
	private BillMapper billMapper;
	
	public BillMapper getBillMapper() {
		return billMapper;
	}

	public void setBillMapper(BillMapper billMapper) {
		this.billMapper = billMapper;
	}

	/*private BillDao billDao;
	
	public BillServiceImpl(){
		billDao = new BillDaoImpl();
	}*/
	@Override
	public boolean add(Bill bill) {
		int count = billMapper.insertBill(bill);
		if(count == 0)
			return false;
		else
			return true;
		
		// TODO Auto-generated method stub
		/*boolean flag = false;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);
			if(billDao.add(connection,bill) > 0)
				flag = true;
			connection.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				System.out.println("rollback==================");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			
			index = session.getMapper(BillMapper.class).insertBill(bill);
			System.out.println("index : " + index);
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		if(index == 0)
			return false;
		else
			return true;*/
	}

	@Override
	public List<Bill> getBillList(Bill bill) {
		List<Bill> billList = billMapper.findBillListByNameAndIdAndIspayment(bill);
		return billList;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		List<Bill> billList = null;
		System.out.println("query productName ---- > " + bill.getProductName());
		System.out.println("query providerId ---- > " + bill.getProviderId());
		System.out.println("query isPayment ---- > " + bill.getIsPayment());
		
		try {
			connection = BaseDao.getConnection();
			billList = billDao.getBillList(connection, bill);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return billList;*/
		/*SqlSession session = null;
		List<Bill> billList = null;
		try {
			session = MyBatisUtil.createSqlSession();
			
			billList = session.getMapper(BillMapper.class).findBillListByNameAndIdAndIspayment(bill);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		
		
		return billList;*/
	}

	@Override
	public boolean deleteBillById(String delId) {
		int count = billMapper.deleteBill(Integer.parseInt(delId));
		if(count == 0)
			return false;
		else
			return true;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(billDao.deleteBillById(connection, delId) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
		*/
		
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();		
			index = session.getMapper(BillMapper.class).deleteBill(Integer.parseInt(delId));	
			System.out.println("index : " + index);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}		
		if(index == 0)
			return false;
		else
			return true;*/
	}

	@Override
	public Bill getBillById(String id) {
		Bill bill = billMapper.findOneBillById(Integer.parseInt(id));
		return bill;
		// TODO Auto-generated method stub
		/*Bill bill = null;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			bill = billDao.getBillById(connection, id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			bill = null;
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return bill;*/
		/*Bill bill = null;
		SqlSession session = null;
		
		try {
			session = MyBatisUtil.createSqlSession();
			
			bill = session.getMapper(BillMapper.class).findOneBillById(Integer.parseInt(id));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return bill;*/
	}

	@Override
	public boolean modify(Bill bill) {
		int count = billMapper.updateBill(bill);
		if(count == 0)
			return false;
		else
			return true;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(billDao.modify(connection,bill) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			
			index = session.getMapper(BillMapper.class).updateBill(bill);
			
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		if(index == 0)
			return false;
		else
			return true;*/
	}

}
